home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-28 | 10.5 KB | 469 lines | [TEXT/PJMM] |
- {P4/Mac port by Ingemar Ragnemalm 1994-1996}
-
- unit EditWindow;
-
- interface
-
- uses
- TransEdit, TransSkel;
-
-
- procedure InitEditWindow;
-
-
- implementation
-
- const
- maxSize = 8; { no. font sizes made available }
- hSize = 300; { horiz, vert size of new windows }
- vSize = 205;
-
- { File menu item numbers }
-
- new = 1; { begin new window }
- open = 2; { open existing file }
- close = 3; { close file }
- save = 5; { save file }
- saveas = 6; { save under another name }
- saveCopy = 7; { save a copy w/o switching file binding }
- revert = 8; { revert to version on disk }
- quit = 10;
-
- { Edit menu item numbers }
-
- undo = 1;
- cut = 3;
- copy = 4;
- paste = 5;
- clear = 6;
-
- { Format menu item numbers }
-
- wordWrap = 1;
- noWrap = 2;
- leftJust = 4;
- centerJust = 5;
- rightJust = 6;
-
- var
- lastFront: WindowPtr; { keeps track of front window }
- fileMenu, editMenu, fontMenu, sizeMenu, formatMenu: MenuHandle;
-
- sizes: array[0..maxSize] of integer;
-
- dummy: Boolean;
-
- windCount: integer; {Used for window offsets}
-
- { Uncheck all the items in a menu}
-
-
- procedure UncheckMenu (theMenu: MenuHandle);
- var
- i, nItems: integer;
-
- begin
- nItems := CountMItems(theMenu);
-
- for i := 1 to nItems do
- begin
- CheckItem(theMenu, i, false);
- SetItemStyle(theMenu, i, []);
- end;
- end;
-
- { Set the Font, Size and Format menus so that the items corresponding}
- { to the text characteristics of the window are checked. If the}
- { window isn't an edit window, dim all three menus.}
-
-
- procedure SetTextMenus (drawBar: Boolean);
-
- var
- theWind: WindowPtr;
- wFontName, mFontName: str255;
- i, nItems: integer;
- te: TEHandle;
-
- begin
- theWind := Frontwindow;
- UncheckMenu(fontMenu); { toss current check marks }
- UncheckMenu(sizeMenu);
- UncheckMenu(formatMenu);
-
- if not IsEWindow(theWind) then { disable the menus }
- begin
- DisableItem(fontMenu, 0);
- DisableItem(sizeMenu, 0);
- DisableItem(formatMenu, 0);
- end
- else
- begin
- EnableItem(fontMenu, 0);
- EnableItem(sizeMenu, 0);
- EnableItem(formatMenu, 0);
-
- te := GetEWindowTE(theWind);
- if te^^.crOnly < 0 then
-
- { Check appropriate word wrap item}
-
- CheckItem(formatMenu, noWrap, true)
- else
- CheckItem(formatMenu, wordWrap, true);
-
- { Check appropriate justification item{}
-
- case te^^.just of
- teJustLeft:
- CheckItem(formatMenu, leftJust, true);
- teJustRight:
- CheckItem(formatMenu, rightJust, true);
- teJustCenter:
- CheckItem(formatMenu, centerjust, true);
- otherwise
- end;
-
- { Check appropriate font name item}
-
- for i := 0 to maxSize - 1 do
- begin
- if te^^.txSize = sizes[i] then
- checkitem(sizeMenu, i + 1, true);
- if RealFont(te^^.txFont, sizes[i]) then
- SetItemStyle(sizeMenu, i + 1, [outline])
- else
- SetItemStyle(sizeMenu, i + 1, []);
- GetFontName(te^^.txFont, wFontName);
- nItems := CountMItems(fontMenu);
- end;
- for i := 1 to nItems - 1 do
- begin
- GetMenuItemText(fontmenu, i, mFontName);
- if EqualString(wFontName, mFontName, false, true) then
- CheckItem(fontMenu, i, true);
- end;
- if drawBar then
- DrawMenuBar;
- end;
- end;
-
- { Set File/Edit menu items according to type of front window.}
-
- { The general behavior is:}
-
- { New and Open always enabled, since a new edit window can always be}
- { opened.}
-
- { Close enabled when an edit or DA window in front (i.e., when there's}
- { a window at all).}
-
- { Save enabled for edit windows not bound to a file, and edit windows}
- { bound to a file when they're dirty (typed into, Edit menu used to}
- { do something to them).}
-
- { Save As and Save a Copy As enabled for edit windows.}
-
- { Revert enabled for edit windows bound to a file when they're dirty.}
-
- { Undo disabled when there's an edit window in front.}
-
- procedure SetNonTextmenus;
-
- var
- theWind: WindowPtr;
- theKind: integer;
- thePeek: windowPeek;
-
- begin
- DisableItem(fileMenu, close); { assume no window at all }
- DisableItem(fileMenu, save);
- DisableItem(fileMenu, saveas);
- DisableItem(fileMenu, savecopy);
- DisableItem(fileMenu, revert);
- EnableItem(editMenu, undo);
-
- theKind := 0;
- theWind := FrontWindow;
- thePeek := WindowPeek(theWind);
- if theWind <> nil then
- theKind := thePeek^.windowKind;
- if theKind < 0 then { DA in front }
- EnableItem(fileMenu, close)
- else if IsEWindow(theWind) then { edit window in front }
- begin
- EnableItem(fileMenu, close);
- EnableItem(fileMenu, saveas);
- EnableItem(fileMenu, savecopy);
- if (GetEWindowFile(theWind, nil) = false) then { not bound to file }
- EnableItem(fileMenu, save)
- else if IsEWindowDirty(theWind) then { bound - is it dirty? }
- begin
- EnableItem(fileMenu, save);
- EnableItem(fileMenu, revert);
- end;
- DisableItem(editMenu, undo);
- end;
- end;
-
- { Background procedure. Check front window, reset menus if it}
- { changes. The menu bar doesn't need redrawing by SetTextMenus}
- { if the previous and current front window are either both edit}
- { windows or both not edit windows. This check eliminates some}
- { needless menu flashing.}
-
- procedure CheckFront;
-
- begin
- if FrontWindow <> lastFront then
- begin
- SetNonTextMenus;
- if IsEWindow(FrontWindow) = IsEwindow(lastFront) then
- SetTextmenus(false)
- else
- SetTextmenus(true);
- lastFront := FrontWindow;
- end;
- end;
-
- { Got an activate or deactivate. It doesn't matter which, really.}
- { Set the text menus appropriately for the front window, and draw}
- { the menu bar, as these menus might change state from enabled to}
- { disabled or vice-versa.}
-
-
- procedure Activate (active: Boolean);
- begin
- CheckFront;
- end;
-
- { Got a keyclick in an edit window.}
-
-
- procedure Key;
- begin
- SetNonTextMenus;
- end;
-
- { Close selected from File menu, or close box of edit window}
- { clicked.}
-
- procedure myClose;
- var
- theWind: WindowPtr;
- ignore: Boolean;
-
- begin
- GetPort(theWind);
- ignore := EWindowClose(theWind);
- CheckFront;
- end;
-
- procedure MakeWind (bindToFile: Boolean);
- var
- r: Rect;
- offset: integer;
- ignore: WindowPtr;
- begin
- if FrontWindow = nil then
- windCount := 0;
- SetRect(r, 0, 0, hSize, vSize);
- windCount := windCount + 1;
- offset := 50 + (25 * (windCount mod 4));
- OffsetRect(r, offset, offset);
- ignore := NewEWindow(r, '', true, WindowPtr(-1), true, longint(0), bindToFile);
- end;
-
-
- procedure AEOpenDocument (spec: FSSpec);
- var
- r: Rect;
- offset: integer;
- ignore: WindowPtr;
- begin
- if FrontWindow = nil then
- windCount := 0;
- SetRect(r, 0, 0, hSize, vSize);
- windCount := windCount + 1;
- offset := 50 + (25 * (windCount mod 4));
- OffsetRect(r, offset, offset);
- ignore := FSpNewEWindow(r, true, WindowPtr(-1), true, 0, spec);
- end;
-
-
- { File menu handler}
-
-
- procedure DoFileMenu (item: integer);
-
- var
- theWind: WindowPtr;
- mypeek: WindowPeek;
- ignore: Boolean;
- begin
- theWind := FrontWindow;
- case item of
- new:
- MakeWind(false);
- open:
- MakeWind(true);
- close:
- if ISEWindow(theWind) then
- ignore := EWindowClose(theWind)
- else
- begin {Non-editing window (Conside, messages…) - just hide it!}
- HideWindow(theWind);
- {mypeek := WindowPeek(theWind);}
- {CloseDeskAcc(mypeek^.windowKind); { DA in front }
- end;
- save:
- ignore := EWindowSave(theWind);
- saveas:
- ignore := EWindowSaveAs(theWind);
- revert:
- ignore := EWindowRevert(theWind);
- quit:
- if ClobberEWindows = true then
- SkelWhoa;
- otherwise
- end;
- SetNonTextMenus;
- end;
-
- { Handle Font menu items}
-
-
- procedure DoFontMenu (item: integer);
-
- var
- font: integer;
- te: TEHandle;
- theWind: WindowPtr;
- theFontName: Str255;
-
- begin
- theWind := FrontWindow;
- te := GetEWindowTE(theWind);
- if te <> nil then { not an edit window }
- begin
- GetMenuItemText(fontMenu, item, theFontName);
- GetFNum(theFontName, font);
- SetEWindowSTyle(theWind, font, te^^.txSize, te^^.crOnly, te^^.just);
- SetTExtMenus(false);
- end;
- end;
-
- { Handle Size menu items }
-
- procedure DoSizeMenu (item: integer);
-
- var
- te: TEHandle;
- theWind: WindowPtr;
- begin
- theWind := FrontWindow;
- te := GetEWindowTE(theWind);
- if te <> nil then
- begin
- SetEWindowStyle(theWind, te^^.txFont, sizes[item - 1], te^^.crOnly, te^^.just);
- SetTextMenus(false);
- end;
- end;
-
- { Handle Format menu items }
-
- procedure DoFormatMenu (item: integer);
-
- var
- font, size, just, wrap: integer;
- te: TEHandle;
- theWind: WindowPtr;
-
- begin
- theWind := FrontWindow;
- te := GetEWindowTE(theWind);
- if te <> nil then { an edit window }
- begin
- font := te^^.txFont;
- size := te^^.txSize;
- just := te^^.just;
- wrap := te^^.crOnly;
- case item of
- wordWrap:
- wrap := 0;
- noWrap:
- wrap := -1;
- leftJust:
- just := teJustLeft;
- centerJust:
- just := teJustCenter;
- rightJust:
- just := teJustRight;
- otherwise
- end;
- SetEWindowStyle(theWind, font, size, wrap, just);
- SetTextMenus(false);
- end;
- end;
-
-
-
-
- procedure InitEditWindow;
- begin
- lastFront := nil;
- sizes[0] := 9;
- sizes[1] := 10;
- sizes[2] := 12;
- sizes[3] := 14;
- sizes[4] := 18;
- sizes[5] := 20;
- sizes[6] := 24;
- sizes[7] := 48;
-
- fileMenu := NewMenu(1000, 'File');
- AppendMenu(fileMenu, 'New/N;Open…/O;Close/W;(-;Save/S;Save As…');
- AppendMenu(fileMenu, 'Save a Copy As…;Revert/R;(-;Quit/Q');
- dummy := SkelMenu(fileMenu, @DoFileMenu, nil, false);
-
- editMenu := NewMenu(1001, 'Edit');
- AppendMenu(editMenu, 'Undo/Z;(-;Cut/X;Copy/C;Paste/V;Clear');
- dummy := SkelMenu(editMenu, @EWindowEditOp, nil, false);
-
- fontMenu := NewMenu(1002, 'Font');
- DisAbleItem(fontMenu, 0);
- AppendResMenu(fontMenu, 'FONT'); {AddResMenu}
- dummy := SkelMenu(fontMenu, @DoFontMenu, nil, false);
- sizeMenu := NewMenu(1003, 'Size');
- DisableItem(sizeMenu, 0);
- AppendMenu(sizemenu, '9 Point;10 Point;12 Point;14 Point');
- AppendMenu(sizeMenu, '18 Point;20 Point;24 Point;48 Point');
- dummy := SkelMenu(sizeMenu, @DoSizeMenu, nil, false);
-
- {formatMenu := NewMenu(1004, 'Format');}
- {DisableItem(formatMenu, 0);}
- {AppendMenu(formatMenu, 'Word Wrap;No Word Wrap;(-;Left;Center;Right');}
- {dummy := Skelmenu(formatMenu, @DoFormatMenu, nil, true);}
-
- SetNonTextMenus;
- SetTextMenus(true);
-
- { Do TransEdit-specific setup: set creator for any files created,}
- { set default text style and event notification procedures for}
- { new windows.}
-
- SetEWindowCreator('p4’M');
- SetEWindowStyle(nil, monaco, 9, 0, teJustLeft);
- SetEWindowProcs(nil, @Key, @Activate, @myClose);
-
- { Process events until user quits,}
- { then clean up and exit}
-
- SkelBackground(@CheckFront);
-
- {Set Apple Event handler callbacks!}
- ESetAEProcs(@AEOpenDocument, nil);
-
- end;
-
- end.